package com.deschen.security.core.social.weixin.connect;

import com.deschen.security.core.social.qq.api.QQ;
import com.deschen.security.core.social.qq.api.QQImpl;
import com.deschen.security.core.social.qq.connect.QQOAuth2Template;
import com.deschen.security.core.social.weixin.api.Weixin;
import com.deschen.security.core.social.weixin.api.WeixinImpl;
import org.springframework.social.oauth2.AbstractOAuth2ServiceProvider;

/**
 * @Author deschen
 * @Create 2019/9/10
 * @Description spring social OAuth2.0 流程代码
 * @Since 1.0.0
 */
// 文档：https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html
public class WeixinServiceProvider extends AbstractOAuth2ServiceProvider<Weixin> {
    private String appId;

    // 将用户导向认证服务器获取授权码链接
    private static final String AUTHORIZE_URL = " https://open.weixin.qq.com/connect/qrconnect";
    // 获取认证服务器授权码申请令牌链接
    private static final String ACCESSTOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token";

    /**
     * OAuth2.0 第一到第五流程
     * 系统级配置，说明是哪个应用
     * @param appId
     * @param appSecret
     */
    public WeixinServiceProvider(String appId, String appSecret) {
        super(new WeixinOAuth2Template(appId, appSecret, AUTHORIZE_URL, ACCESSTOKEN_URL));
        this.appId = appId;
    }

    /**
     * OAuth2.0 获取微信用户信息
     * @param accessToken
     * @return
     */
    @Override
    public Weixin getApi(String accessToken) {
        return new WeixinImpl(accessToken);
    }
}
